<?php
/**
 * Tools for basics operations
 *
 * @package Dev-Site
 * @author Gonçalo Margalho <gsky89@gmail.com>
 * @copyright Dev-House.Com (C) 2006-2008
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License
 * @version 1.0
 */
class tools{

	/**
	 * Anti html injection
	 *
	 * @param string $str string of text to check
	 * @param boolean $bbcode if check also bbcode
	 * @retun string
	 */
	public function input($str, $bbcode = false){
		if($bbcode == true)
			$str = $this->bbcode($str);
		else{ $str = str_replace  ( "&gt;", ">", $str ); $str = str_replace  ( "&lt;", "<", $str ); }


		if(get_magic_quotes_gpc())
			return $str;
		else
			return @mysql_escape_string($str);
	}
	
	/**
	 * Transfer BBCODE in HTML
	 *
	 * @param string $str String to convert
	 * @return string
	 */
	public static function bbcode($str){
		$str = htmlspecialchars($str);

		$bbcode_search = array(
					'/\[b\](.*?)\[\/b\]/is',                                
					'/\[i\](.*?)\[\/i\]/is',                                
					'/\[u\](.*?)\[\/u\]/is',                                
					'/\[url\=(.*?)\](.*?)\[\/url\]/is',                         
					'/\[url\](.*?)\[\/url\]/is',                             
					'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',    
					'/\[img\](.*?)\[\/img\]/is',                            
					'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',                    
					'/\[mail\](.*?)\[\/mail\]/is',                            
					'/\[font\=(.*?)\](.*?)\[\/font\]/is',                    
					'/\[size\=(.*?)\](.*?)\[\/size\]/is',                    
					'/\[color\=(.*?)\](.*?)\[\/color\]/is',
					'/\[quote\=(.*?)\](.*?)\[\/quote\]/is', 
					'/\[quote\](.*?)\[\/quote\]/is',
					);

		$bbcode_replace = array(
					'<strong>$1</strong>',
					'<em>$1</em>',
					'<u>$1</u>',
					'<a href="$1">$2</a>',
					'<a href="$1">$1</a>',
					'<div style="text-align: $1;">$2</div>',
					'<img src="$1" alt="$1"/>',
					'<a href="mailto:$1">$2</a>',
					'<a href="mailto:$1">$1</a>',
					'<span style="font-family: $1;">$2</span>',
					'<span style="font-size: $1;">$2</span>',
					'<span style="color: $1;">$2</span>',
					'<br />$1 <blockquote>$2</blockquote>',
					'<blockquote>$1</blockquote>',
					);


		$str = preg_replace($bbcode_search, $bbcode_replace, $str);
		return nl2br($str);
	}

	public static function html($str){
		$bbcode_replace = array(
					'[b]$1[/b]',                                
					'[i]$1[/i]',                                
					'[u]$1\[/u]',                                
					'[url=$1]$2[/url]',                         
					'[url]$1[/url]',                             
					'[align=$1]$2[/align]',    
					'[img]$1[/img]',                            
					'[mail=$1]$2[/mail]',                    
					'[mail]$1[/mail]',                            
					'[font=$1]$2[/font]',                    
					'[size=$1]$2[/size]',                    
					'[color=$1]$2[/color]',
					'[quote=$1]$2[/quote]', 
					'[quote]$1[/quote]',
					);

		$bbcode_search = array(
					'/\<strong\>(.*?)\<\/strong\>/is',
					'/\<em\>(.*?)\<\/em\>/is',
					'/\<u\>(.*?)\<\/u\>/is',
					'/\<a href\=\"(.*?)\"\>(.*?)\<\/a\>/is',
					'/\<a href\=\"(.*?)\"\>(.*?)\<\/a\>/is',
					'/\<div style\=\"text-align\: (.*?)\;\"\>(.*?)\<\/div\>/is',
					'/\<img src\=\"(.*?)\" alt\=\"(.*?)\"\/\>/is',
					'/\<a href\=\"mailto\:(.*?)\"\>(.*?)\<\/a\>/is',
					'/\<a href\=\"mailto\:(.*?)\"\>(.*?)\<\/a\>/is',
					'/\<span style\=\"font\-family\: (.*?)\;\"\>(.*?)\<\/span\>/is',
					'/\<span style\=\"font\-size\: (.*?)\;\"\>(.*?)\<\/span\>/is',
					'/\<span style\=\"color\: (.*?)\;\"\>(.*?)\<\/span\>/is',
					'/\<br \/\>(.*?) \<blockquote\>(.*?)\<\/blockquote\>/is',
					'/\<blockquote\>(.*?)\<\/blockquote\>/is',
					);
		$str = preg_replace($bbcode_search, $bbcode_replace, $str);
		return nl2br($str);
	}

	public static function checkEmail($email){
		return eregi("^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$", $email);
	}

	/**
	 * Add
	 * @param <type> $text
	 * @return <type>
	 */
	function clickable($text){
		$ret = ' ' . $text;
		$ret = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" target=\"_blank\">'.acapo('\\2').'</a>'", $ret);
		$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" target=\"_blank\">'.acapo('\\2').'</a>'", $ret);

		$ret = substr($ret, 1);

		return $ret;
	}
}

?>